Ext.define('Functions', {
    extend: 'Ext.data.Model',
    fields: [
        "Id"
		, "Name"   //名称
		, "OrderIndex"   //序号
		, "Value"   //权限
		, "Url"   //连接地址
		, "ModuleID"   //模块ID
		, "Image"   //图标

    ],
    idProperty: "Id"
})

Ext.define('SHApp.System.Functions.Grid', {
    extend: 'BLBTSystem.grid',
    alias: 'widget.systemfunctiongrid',
    //requires: ['SHApp.System.Functions.Form'],
    dataModel: "Functions",
    dataUrl:"api/function?format=json",
    hasPageBar: false,
    upID: 'up',
    downID: 'down',
    moduleId: '',
    initComponent: function () {
        var me = this;
        me.btnAddID += me.id;
        me.btnEditID += me.id;
        me.btnDeleteID += me.id;
        me.upID += me.id;
        me.downID += me.id;
        this.creatColumns();
        this.creatTbar();
        Ext.apply(this);
        this.callParent(arguments);
        this.checkBtnPermission(me.pvalue);
    },
    creatColumns: function () {
        var me = this;
        me.columns = [{
            text: "Id",
            flex: 1,
            dataIndex: 'Id',
            sortable: false,
            hidden: true
        }, {
            text: "序号",
            width: 60,
            dataIndex: 'OrderIndex',
            sortable: true
        }, {
            text: "名称",
            width: 150,
            dataIndex: 'Name',
            sortable: true
        }, {
            text: "权限",
            width: 100,
             hidden: true,
            dataIndex: 'Value',
            sortable: true
        }, {
            text: "连接地址",
            width: 300,
            dataIndex: 'Url',
            sortable: true
        }, {
            text: "模块ID",
            width: 100,
            dataIndex: 'ModuleID',
            sortable: true,
            hidden: true
        }, {
            text: "图标",
            width: 180,
            dataIndex: 'Image',
            sortable: true
        }
		]
    },
    loadByModuleID: function (id) {
        var me = this;
        me.moduleId = id;
        var store = me.store;
        store.proxy.url = me.dataUrl + "&ModuleID=" + id + "&";
        store.load();
    },
    creatTbar: function () {
        var me = this;
        me.tbar = ['->', {
            xtype: 'button',
            text: '上移',
            id: me.upID,
            iconCls: 'icon-btn-up',
            handler: function () {
                var rs = me.getSelectionModel().getSelection();
                if (rs.length == 0) {
                    Ext.Msg.alert("提示", "请选择一行再操作!");
                    return;
                }
                var store = me.store;
                var rsID = rs[0].data.Id;
                var orderIndex = 0;
                var items = store.data.items;
                if (store.data.keys[0] == rs[0].data.Id) {
                    Ext.Msg.alert("提示", "已经是第一行了！");
                    return;
                }
                for (var i = 0; i < items.length; i++) {
                    if (items[i].data.Id == rsID) {
                        orderIndex = items[i - 1].data.OrderIndex;
                        items[i - 1].data.OrderIndex = items[i].data.OrderIndex;
                        items[i].data.OrderIndex = orderIndex;
                        Ext.Ajax.request({
                            method: "POST",
                            url: me.dataUrl,
                            jsonData: items[i - 1].data
                        })
                        Ext.Ajax.request({
                            method: "POST",
                            url: me.dataUrl,
                            jsonData: items[i].data,
                            success: function (response) {
                                me.store.load();
                            }
                        })
                        break;
                    }
                }
            }
        }, {
            xtype: 'button',
            text: '下移',
            id: me.downID,
            iconCls: 'icon-btn-down',
            handler: function () {
                var rs = me.getSelectionModel().getSelection();
                if (rs.length == 0) {
                    Ext.Msg.alert("提示", "请选择一行再操作!");
                    return;
                }
                var store = me.store;
                var rsID = rs[0].data.Id;
                var orderIndex = 0;
                var items = store.data.items;
                if (store.data.keys[store.data.keys.length - 1] == rs[0].data.Id) {
                    Ext.Msg.alert("提示", "已经是最后一行了！");
                    return;
                }
                for (var i = 0; i < items.length; i++) {
                    if (items[i].data.Id == rsID) {
                        orderIndex = items[i + 1].data.OrderIndex;
                        items[i + 1].data.OrderIndex = items[i].data.OrderIndex;
                        items[i].data.OrderIndex = orderIndex;
                        Ext.Ajax.request({
                            method: "POST",
                            url: me.dataUrl,
                            jsonData: items[i + 1].data
                        })
                        Ext.Ajax.request({
                            method: "POST",
                            url: me.dataUrl,
                            jsonData: items[i].data,
                            success: function (response) {
                                me.store.load();
                            }
                        })
                        break;
                    }
                }
            }
        }, {
            xtype: 'button',
            text: '添加',
            id: me.btnAddID,
            iconCls: 'icon-btn-add',
            handler: function () {
                var win = me.getForm('SHApp.Menu.Form');
                if (me.moduleId == "") {
                    Ext.Msg.alert("提示", "请先选择模块后再添加功能！");
                    return;
                }
                win.loadRecord(new Menu({ Id: null, ModuleID: me.moduleId, Value: 15 }));
                win.Show("添加");
            }
        }, {
            xtype: 'button',
            disabled: true,
            id: me.btnEditID,
            text: '修改',
            iconCls: 'icon-btn-edit',
            handler: function () {
                me.EditData(me.getForm('SHApp.Menu.Form'));
            }
        }, {
            xtype: 'button',
            disabled: true,
            id: me.btnDeleteID,
            text: '删除',
            iconCls: 'icon-btn-delete',
            handler: function () {
                me.DeleteData();
            }
        }
        ]
    }
})

